package org.example.json;

import com.google.common.base.Preconditions;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@Warmup(iterations = 5)
@Measurement(iterations = 5)
@BenchmarkMode({Mode.Throughput, Mode.AverageTime})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(5)
public class JSONParseBenchmark {

  private static final String json = "{\"requestId\":\"0256fbae906a40068bc38eed57c69706\",\"name\":\"张三\",\"idCard\":\"131023188812121234\",\"phone\":\" 11122223333\"}";

  private static final FastjsonTest fastjsonTest = new FastjsonTest();
  private static final GsonTest gsonTest = new GsonTest();
  private static final JacksonTest jacksonTest = new JacksonTest();

  @Benchmark
  @Threads(10)
  public void testFastJSONParseClass() {
    Preconditions.checkArgument(fastjsonTest.getRequestFromParam(json).equals("0256fbae906a40068bc38eed57c69706"));
  }

  @Benchmark
  @Threads(10)
  public void testFastJSONParseMap() {
    Preconditions.checkArgument(fastjsonTest.getRequestFromMap(json).equals("0256fbae906a40068bc38eed57c69706"));
  }

  @Benchmark
  @Threads(10)
  public void testGsonParse() {
    Preconditions.checkArgument(gsonTest.getRequestFromParam(json).equals("0256fbae906a40068bc38eed57c69706"));
  }

  @Benchmark
  @Threads(10)
  public void testJacksonParse() {
    Preconditions.checkArgument(jacksonTest.getRequestFromParam(json).equals("0256fbae906a40068bc38eed57c69706"));
  }

  public static void main(String[] args) throws RunnerException {
    Options opt = new OptionsBuilder()
        .include(JSONParseBenchmark.class.getSimpleName())
        .forks(1)
        .build();
    new Runner(opt).run();
  }
}
